package cn.v2tila.rsosa.management.controller;

import cn.v2tila.rsosa.management.entity.AssoEntity;
import cn.v2tila.rsosa.management.entity.StudentEntity;
import cn.v2tila.rsosa.management.jpa.AssoJPA;
import cn.v2tila.rsosa.management.service.AssoService;
import cn.v2tila.rsosa.management.service.CollegeService;
import cn.v2tila.rsosa.management.service.SchoolService;
import cn.v2tila.rsosa.management.service.StudentService;
import cn.v2tila.rsosa.management.utils.PageDataResult;
import cn.v2tila.rsosa.management.vo.ResultVO;
import cn.v2tila.rsosa.management.vo.editVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @author v2tila on 9:10 2019/4/18
 */
@Controller
@RequestMapping(value = "asso")
public class assoController {
    @Autowired
    private AssoService assoService;
    @Autowired
    private SchoolService schoolService;
    @Autowired
    private CollegeService collegeService;
    @Autowired
    private StudentService studentService;

    @RequestMapping(value = "assoInfo")
    public String toAssoInfo() {
        return "asso/assoInfo";
    }

    @GetMapping(value = "getAsso")
    @ResponseBody
    public PageDataResult getAsso(@RequestParam("page") Integer page,
                                   @RequestParam("limit") Integer limit,
                                   AssoEntity assoEntity){
        if(assoEntity.getUniqueId().equals("")){
            assoEntity.setUniqueId(null);
        }

        return assoService.getAsso(page, limit,assoEntity);
    }
    @PostMapping(value = "addAsso")
    @ResponseBody
    public String addAsso(@RequestBody AssoEntity assoEntity) {
        String schoolNo = assoEntity.getSchoolNo();
        String collegeNo = assoEntity.getCollegeNo();
        String no = assoEntity.getNo();
        if(schoolService.findByNo(schoolNo)==null){
            return "没有该学校";
        }else{
            if(collegeService.findBySchoolNoAndNo(schoolNo,collegeNo)==null){
                return "没有该学院";
            }else {
                if(assoService.existsByNo(no)){
                    return "该编号已存在";
                }else{
                    try {
                        AssoEntity saveIsSuccess = assoService.saveAndFlush(assoEntity);
                        if (saveIsSuccess == null) {
                            return "存储失败";
                        }else {
                            return "ok";
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        return "操作异常，请您稍后再试";
                    }
                }
            }
        }

    }

    @PostMapping(value = "delAsso")
    @ResponseBody
    public ResultVO delAsso(@RequestBody @RequestParam(value = "id") Integer id){
        ResultVO resultVO = new ResultVO();
        try {
            assoService.deleteById(id);
            resultVO.setCode(1);
        } catch (Exception e) {
            resultVO.setCode(0);
            resultVO.setMsg("删除失败");
        }

        return resultVO;
    }

    @PostMapping(value = "updateUniqueId")
    @ResponseBody
    public ResultVO updateUniqueId(@RequestBody editVO editVO) {
        ResultVO resultVO = new ResultVO();
        int id = editVO.getId();
        String value = editVO.getValue();
        StudentEntity studentEntity = studentService.findByUniqueId(value);
        if(studentEntity==null){
            resultVO.setMsg("该学生不存在");
            resultVO.setCode(0);
        }else {
            AssoEntity assoEntity = assoService.findById(id);
            assoEntity.setUniqueId(value);
            AssoEntity saveIsSuccess = assoService.saveAndFlush(assoEntity);
            if(saveIsSuccess==null){
                resultVO.setCode(0);
                resultVO.setMsg("存储失败");
            }else{
                resultVO.setCode(1);

            }
        }

        return resultVO;
    }
}
